package Visual;

import Logica.Cliente;
import Logica.Empleado;
import Logica.Importancia;
import Logica.Repuesto;
import Logica.Vehiculo;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;

public class frmAltaPedidoRepuesto extends javax.swing.JInternalFrame {

    ControladoraVisual Visual = new ControladoraVisual();
    DefaultComboBoxModel modeloCliente = new DefaultComboBoxModel();
    DefaultComboBoxModel modeloVehiculo = new DefaultComboBoxModel();
    DefaultComboBoxModel modeloImportancia = new DefaultComboBoxModel();
    DefaultComboBoxModel modeloRepuesto = new DefaultComboBoxModel();
    DefaultComboBoxModel modeloEmpleado = new DefaultComboBoxModel();
    
    public frmAltaPedidoRepuesto() {
        initComponents();
        cargarComboCliente();
        cargarRepuestos();
        
        cmbVehiculo.setModel(null);
    }

    public frmAltaPedidoRepuesto(ControladoraVisual Visual) {
        initComponents();
        this.Visual = Visual;
        cargarComboCliente();
        cargarComboImportancia();
        cargarEmpleados();
        cargarRepuestos();
    }

    public void cargarEmpleados(){
        List<Empleado> empleados = Visual.dameEmpleados();
        for(Empleado unEmpleado:empleados){
            this.modeloEmpleado.addElement(unEmpleado);
        }
        this.cmbEmpleado.setModel(modeloEmpleado);
    
    }
    public void cargarComboImportancia() {
        List<Importancia> importancias = Visual.dameImportancias();
        for (Importancia unaImportancia : importancias) {
            modeloImportancia.addElement(unaImportancia);
        }
        cmbImportancia.setModel(modeloImportancia);
    }

    public void cargarRepuestos(){
        List<Repuesto> repuestos = Visual.dameRepuestos();
        for(Repuesto unRepuesto : repuestos){
            modeloRepuesto.addElement(unRepuesto);
        }
        cmbRepuesto.setModel(modeloRepuesto);
    }
    
    public void cargarComboCliente() {
        List<Cliente> clientes = Visual.dameClientes();
        for (Cliente unCliente : clientes) {
            modeloCliente.addElement(unCliente);
        }
        cmbCliente.setModel(modeloCliente);
    }

    public void cargarComboVehiculo() {
        List<Vehiculo> vehiculos = Visual.dameVehiculos();
        Cliente cliente = (Cliente) cmbCliente.getSelectedItem();
        for (Vehiculo unVehiculo : vehiculos) {
            if (unVehiculo.getUnCliente().getNroDoc() == cliente.getNroDoc()) {
                modeloVehiculo.addElement(unVehiculo);
            }
        }
        cmbVehiculo.setModel(modeloVehiculo);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtNroPedido = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();
        btnAlta = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        chkAutorizado = new javax.swing.JCheckBox();
        chkRecambio = new javax.swing.JCheckBox();
        lblnumero = new javax.swing.JLabel();
        lblnumero1 = new javax.swing.JLabel();
        cmbCliente = new javax.swing.JComboBox();
        lblnumero2 = new javax.swing.JLabel();
        cmbVehiculo = new javax.swing.JComboBox();
        btnCargar = new javax.swing.JButton();
        lblnumero3 = new javax.swing.JLabel();
        cmbImportancia = new javax.swing.JComboBox();
        cmbRepuesto = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cmbEmpleado = new javax.swing.JComboBox();

        jLabel1.setText("Nro de Pedido");

        jLabel2.setText("Descripción");

        jLabel3.setText("Cantidad");

        jLabel4.setText("Autorizado");

        jLabel5.setText("Recambio");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnAlta.setText("Alta");
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        lblnumero.setText("Nro Repuesto");

        lblnumero1.setText("Cliente:");

        cmbCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbClienteActionPerformed(evt);
            }
        });

        lblnumero2.setText("Vehiculo:");

        cmbVehiculo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));

        btnCargar.setText("Cargar Vehiculos");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        lblnumero3.setText("Importancia:");

        cmbRepuesto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbRepuestoActionPerformed(evt);
            }
        });

        jLabel6.setText("Empleado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(13, 13, 13)
                        .addComponent(txtCantidad))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAlta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblnumero1)
                        .addGap(40, 40, 40)
                        .addComponent(cmbCliente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblnumero2)
                        .addGap(34, 34, 34)
                        .addComponent(cmbVehiculo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCargar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblnumero3)
                        .addGap(16, 16, 16)
                        .addComponent(cmbImportancia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblnumero)
                        .addGap(11, 11, 11)
                        .addComponent(cmbRepuesto, 0, 191, Short.MAX_VALUE))
                    .addComponent(txtDescripcion)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNroPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(chkAutorizado))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(chkRecambio)))
                            .addComponent(jLabel2))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(31, 31, 31)
                        .addComponent(cmbEmpleado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNroPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblnumero)
                    .addComponent(cmbRepuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblnumero1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCargar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblnumero2, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbVehiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblnumero3, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbImportancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cmbEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chkAutorizado, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(chkRecambio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAlta))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed
        int nroPedido = Integer.valueOf(this.txtNroPedido.getText());
        Repuesto repuesto = (Repuesto) cmbRepuesto.getSelectedItem();
        String descripcion = txtDescripcion.getText();
        int cantidadPedida = Integer.valueOf(this.txtCantidad.getText());
        boolean autorizado = chkAutorizado.isSelected();
        boolean recambio = chkRecambio.isSelected();
        Cliente cliente = (Cliente) cmbCliente.getSelectedItem();
        Vehiculo vehiculo = (Vehiculo) cmbVehiculo.getSelectedItem();
        Importancia importancia = (Importancia) cmbImportancia.getSelectedItem();
        Empleado empleado = (Empleado) cmbEmpleado.getSelectedItem();
        
        try {
            Visual.AltaPedidoRepuesto(nroPedido, descripcion, autorizado, recambio, cantidadPedida, importancia, empleado,repuesto,vehiculo);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaPedidoRepuesto.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaPedidoRepuesto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnAltaActionPerformed

    private void cmbClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbClienteActionPerformed
        modeloVehiculo.removeAllElements();
    }//GEN-LAST:event_cmbClienteActionPerformed

    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        cargarComboVehiculo();
    }//GEN-LAST:event_btnCargarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void cmbRepuestoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbRepuestoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbRepuestoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnSalir;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.JCheckBox chkAutorizado;
    private javax.swing.JCheckBox chkRecambio;
    private javax.swing.JComboBox cmbCliente;
    private javax.swing.JComboBox cmbEmpleado;
    private javax.swing.JComboBox cmbImportancia;
    private javax.swing.JComboBox cmbRepuesto;
    private javax.swing.JComboBox cmbVehiculo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblnumero;
    private javax.swing.JLabel lblnumero1;
    private javax.swing.JLabel lblnumero2;
    private javax.swing.JLabel lblnumero3;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtNroPedido;
    // End of variables declaration//GEN-END:variables
}
